from django.contrib import admin

# Register your models here.

from import_export import resources
from import_export.admin import ImportExportModelAdmin
from wechatpy.enterprise import WeChatClient
from wechatpy.enterprise.crypto import WeChatCrypto
from wechatpy.exceptions import InvalidSignatureException
import itkf as config
import os
from apps.services import models

admin.site.site_title = "企业号后台管理"
admin.site.site_header = "企业号后台管理"

# 企业号ID
ENV_PROFILE = os.getenv("ENV")
if ENV_PROFILE == "test":
    import itkf.test_settings as config
elif ENV_PROFILE == "production":
    import itkf.prd_settings as config
else:
    import itkf.settings as config


class agentResource(resources.ModelResource):
    def get_export_headers(self):
        # 是你想要的导出头部标题headers
        return ['应用名称', '欢迎语', '会话提示语', '部门Logo', '创建时间', '修改时间', '创建者', '修改者']

    class Meta:
        field = ('name', 'welcomeText', 'firstText', 'avatar', 'createTime', 'lastTime', 'author', 'editor')
        model = models.agent
        fields = field
        export_order = field


@admin.register(models.agent)
class agentAdmin(ImportExportModelAdmin):
    fields = (
        'name', 'avatar', 'agentid', 'secret', 'webhook_url',
        'conversationTime')
    # 需要显示的字段信息
    list_display = ('showAvatar', 'name', 'webhook_url', 'conversationTime',
                    'createTime',
                    'lastTime', 'author',
                    'editor',
                    'checkMessage')
    exclude = ('author', 'editor')
    # 设置哪些字段可以点击进入编辑界面，默认是第一个字段
    list_display_links = ('showAvatar', 'name',)
    model_icon = "fa fa-tag"
    list_per_page = 10
    resource_class = agentResource

    def save_model(self, request, obj, form, change):
        if form.is_valid():
            if not change:
                obj.author = request.user
            obj.editor = request.user
            obj.save()
        super().save_model(request, obj, form, change)


class KFResource(resources.ModelResource):
    def get_export_headers(self):
        # 是你想要的导出头部标题headers
        return ['姓名', 'UM', '头像']

    class Meta:
        field = ('username', 'userid', 'sign', 'avatar',)
        model = models.KF
        fields = field
        export_order = field


@admin.register(models.KF)
class KFAdmin(ImportExportModelAdmin):
    fields = ("agent", "avatar", "username", 'userid')
    # 需要显示的字段信息
    list_display = ("agent", "showAvatar", 'username', 'userid', 'status', 'createTime', 'lastTime', 'author', 'editor')
    exclude = ('status',)
    # 设置哪些字段可以点击进入编辑界面，默认是第一个字段
    list_display_links = ('username',)
    model_icon = "fa fa-tag"
    list_per_page = 10
    resource_class = KFResource

    def save_model(self, request, obj, form, change):
        if form.is_valid():
            if not change:
                obj.author = request.user
            obj.editor = request.user
            obj.save()
        super().save_model(request, obj, form, change)


class userListResource(resources.ModelResource):
    def get_export_headers(self):
        # 是你想要的导出头部标题headers
        return ['姓名', 'UM', '头像']

    class Meta:
        field = ('username', 'userid', 'avatar', 'createTime', 'lastTime', 'author', 'editor')
        model = models.userList
        fields = field
        export_order = field


@admin.register(models.userList)
class userListAdmin(ImportExportModelAdmin):
    fields = ('avatar', 'username', 'userid', 'islead',)
    # 需要显示的字段信息
    list_display = ('showAvatar', 'username', 'userid', 'islead', 'createTime', 'lastTime', 'author', 'editor')
    # 设置哪些字段可以点击进入编辑界面，默认是第一个字段
    list_display_links = ('username',)
    search_fields = ('username', 'userid')
    model_icon = "fa fa-tag"
    list_per_page = 50
    resource_class = userListResource

    def save_model(self, request, obj, form, change):
        if form.is_valid():
            if not change:
                obj.author = request.user
            obj.editor = request.user
            obj.save()
        super().save_model(request, obj, form, change)


class MessageResource(resources.ModelResource):
    def get_export_headers(self):
        # 是你想要的导出头部标题headers
        return ['企业号ID', '发送者', '发送时间', '消息ID', '应用ID', '消息类型', '消息内容']

    class Meta:
        field = ('FromUserName', 'CreateTime', 'MsgId', 'AgentID', 'MsgType', 'content')
        model = models.Message
        fields = field
        export_order = field


# Register your models here.
@admin.register(models.Message)
class MessageAdmin(ImportExportModelAdmin):
    fields = ('FromUserName', 'CreateTime', 'MsgId', 'AgentID', 'MsgType', 'content')
    # 需要显示的字段信息
    list_display = (
    'id', 'ToUserName', 'FromUserName', 'CreateTime', 'MsgId', 'AgentID', 'MsgType', 'content', 'author')
    # 设置哪些字段可以点击进入编辑界面，默认是第一个字段
    list_display_links = ('id', 'FromUserName')
    model_icon = "fa fa-tag"
    list_per_page = 10
    resource_class = MessageResource


class knowledgeBaseResource(resources.ModelResource):
    def get_export_headers(self):
        # 是你想要的导出头部标题headers
        return ['问题类型', '关键字', '规则', '发送类型', '消息内容', '创建时间', '修改时间', '创建者', '修改者']

    class Meta:
        field = ('questionType', 'key', 'rule', 'answerType', 'content', 'createTime', 'lastTime', 'author', 'editor')
        model = models.knowledgeBase
        fields = field
        export_order = field


# Register your models here.
@admin.register(models.knowledgeBase)
class knowledgeBaseAdmin(ImportExportModelAdmin):
    fields = ('questionType', 'key', 'rule', 'answerType', 'content')
    # 需要显示的字段信息
    list_display = ('id', 'questionType', 'key', 'rule', 'answerType', 'content')
    # 设置哪些字段可以点击进入编辑界面，默认是第一个字段
    list_display_links = ('id', 'questionType')
    model_icon = "fa fa-tag"
    list_per_page = 10
    resource_class = knowledgeBaseResource

    def save_model(self, request, obj, form, change):
        if form.is_valid():
            if not change:
                obj.author = request.user
            obj.editor = request.user
            obj.save()
        super().save_model(request, obj, form, change)


class staffServiceResource(resources.ModelResource):
    class Meta:
        field = (
            'agent', 'title', 'avatar', 'welcomeText', 'firstText', 'notuserText', "desc", 'createTime', 'lastTime',
            'author', 'editor')
        model = models.staffService
        fields = field
        export_order = field


# Register your models here.
@admin.register(models.staffService)
class staffServiceBaseAdmin(ImportExportModelAdmin):
    fields = ('agent', 'title', 'avatar', "desc", 'welcomeText', 'firstText', 'notuserText',)
    # 需要显示的字段信息
    list_display = (
        'showAvatar', 'agent', 'title', "desc", 'welcomeText', 'firstText', 'notuserText', 'createTime', 'lastTime',
        'author', 'editor')
    # 设置哪些字段可以点击进入编辑界面，默认是第一个字段
    list_display_links = ('agent', 'title',)
    model_icon = "fa fa-tag"
    list_per_page = 10
    resource_class = staffServiceResource

    def save_model(self, request, obj, form, change):
        if form.is_valid():
            if not change:
                obj.author = request.user
            obj.editor = request.user
            obj.save()
        super().save_model(request, obj, form, change)